package com.sofwin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.sofwin.Constants;

public class LoginInterceptor implements HandlerInterceptor {

	/**
	 * @return true放行请求
	 *         false 拦截请求
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean status = false;
		// 登录请求中向session放key currentUser
		// 检查当前session中是否有key为currentUser
		HttpSession session = request.getSession();
		Object obj = session.getAttribute(Constants.CURRENT_USER);
		if(obj!=null) {
			status = true;
			return status;
		}
		response.sendRedirect(request.getContextPath()+"/login.jsp");
		return status;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}

}
